<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of facturacion_controller
 *
 * @author jorge-luis
 */
class facturacion_controller extends CI_Controller {

    public $detalles = array();
    public $factura_model = array();

    function __construct() {
        parent::__construct();
        session_start();
        $this->load->model('servicio');
        $this->load->library('form_validation');
        $this->load->helper(array('url', 'form'));
        $this->load->model('factura');
        $this->load->model('cita');
        $this->load->model('paciente');
        $this->load->model('detalle_factura');
        $this->load->library('session');
    }

    function imprimir() {
        $this->load->library('pdf');
        $factura = $this->factura->modificar($_SESSION['factura_id']);
        $iva = $factura['subtotal'] * ($factura['tarifa_iva'] / 100);
        $paciente = $this->paciente->obtener($factura['paciente_id']);
        $detalles = $this->detalle_factura->buscarByFactura($_SESSION['factura_id']);
        $this->pdf = new Pdf();
        ob_end_clean();
        $this->pdf->AddPage();

        $this->pdf->Cell(15, 2, utf8_decode('Número de Factura:'));
        $this->pdf->Cell(20);
        $this->pdf->Cell(15, 2, $factura['numero']);
        $this->pdf->Ln(5);
        $this->pdf->Cell(15, 5, 'Nombres:');
        $this->pdf->Cell(20);
        $this->pdf->Cell(15, 5, utf8_decode($paciente['nombres']));
        $this->pdf->Ln(2);
        $this->pdf->Cell(15, 7, 'Apellidos:');
        $this->pdf->Cell(20);
        $this->pdf->Cell(15, 7, utf8_decode($paciente['apellidos']));
        $this->pdf->Ln(2);
        $this->pdf->Cell(15, 9, 'RUC:');
        $this->pdf->Cell(20);
        $this->pdf->Cell(15, 9, utf8_decode($paciente['numero_identificacion']));
        $this->pdf->Ln(10);
        $this->pdf->Cell(45, 25, 'CANTIDAD');
        $this->pdf->Cell(45, 25, 'DESCRIPCION');
        $this->pdf->Cell(45, 25, 'TOTAL');
        $this->pdf->Ln(4);

        foreach ($detalles as $detalle) {
            $this->pdf->Cell(45, 25, $detalle['cantidad']);
            $this->pdf->Cell(45, 25, utf8_decode($detalle['descripcion']));
            $this->pdf->Cell(45, 25, $detalle['total']);
            $this->pdf->Ln(2);
        }
        $this->pdf->Ln(4);
        $this->pdf->Cell(45);
        $this->pdf->Cell(45, 25, utf8_decode('Subtotal:'));
        $this->pdf->Cell(2);
        $this->pdf->Cell(45, 25, $factura['subtotal']);
        $this->pdf->Ln(4);
        $this->pdf->Cell(45);
        $this->pdf->Cell(45, 25, utf8_decode('Iva 12:'));
        $this->pdf->Cell(2);
        $this->pdf->Cell(45, 25, $iva);
        $this->pdf->Ln(4);
        $this->pdf->Cell(45);
        $this->pdf->Cell(45, 25, utf8_decode('Total'));
        $this->pdf->Cell(2);
        $this->pdf->Cell(45, 25, $factura['total']);
        //IMPRIMIR EN PDF
        $this->pdf->Output("Factura-" . $factura['numero'] . ".pdf", 'I');
        //MOSTRAR EN NAVEGADOR.
//        $this->pdf->Output();
    }

    function crear() {
        $data['title'] = 'facturar';
        $data['main'] = 'frm_create_factura';
        $this->load->view('/inicio/header', $data);
        $this->factura_model = new $this->factura();
        $id = $this->uri->segment(3);
        $id_paciente = $this->uri->segment(4);
        $_SESSION['cita_id'] = $this->uri->segment(5);
        $servicios = $this->servicio->buscarByEspecialidadMedico($id);
        foreach ($servicios as $servicio) {
            $detalle = new $this->detalle_factura();
            $detalle->descripcion = $servicio['nombre'];
            $detalle->cantidad = 1;
            $detalle->pvp = $servicio['pvp'];
            $detalle->servicio_id = $servicio['id'];
            $this->factura_model->suman+=$detalle->pvp;
            $detalle->total = $detalle->calcula_total($detalle->cantidad, $detalle->pvp);
            array_push($this->detalles, $detalle);
        }
        $this->session->set_userdata('detalles', $this->detalles);
        $this->factura_model->paciente_id = $id_paciente;
        $this->factura_model->subtotal = $this->factura_model->suman;
        $this->factura_model->tarifaIva = $this->factura_model->subtotal * 0.12;
        $this->factura_model->total = $this->factura_model->subtotal + $this->factura_model->tarifaIva;
        $data['detalles'] = $this->detalles;
        $numero = 0;
        foreach ($this->factura->numero() as $value) {
            $numero = $value['numero'];
        }
        $this->factura_model->numero = $numero + 1;
        $data['factura'] = $this->factura_model;
        $data['paciente'] = $this->paciente->obtener($id_paciente);
        $data['servicios'] = $servicios;
        $this->load->view('frm_principal_admin', $data);
    }

    function modificar() {
        $id = $this->uri->segment(3);
        $_SESSION['factura_id'] = $id;
        $factura = $this->factura->modificar($id);
        $data['factura'] = $factura;
        $data['detalles'] = $this->detalle_factura->buscarByFactura($id);
        $data['paciente'] = $this->paciente->obtener($factura['paciente_id']);
        $data['title'] = 'editar Factura';
        $data['main'] = 'frm_edit_factura';
        $this->load->view('/inicio/header', $data);
        $this->load->view('frm_principal_admin', $data);
    }

    function grabar() {
        $data = array(
            'paciente_id' => $this->input->post("paciente_id"),
            'suman' => $this->input->post("suman"),
            'subtotal' => $this->input->post("subtotal"),
            'total' => $this->input->post("total"),
            'numero' => $this->input->post("numero"),
        );
        $data['descuento'] = 0;
        $data['tarifa_iva'] = 12;
        $data['fecha'] = date('Y-m-d H:i:s');
        $data['estado'] = 0;
        $id = $this->factura->grabar($data);
        $dataCita['estado'] = 0;
        $dataCita['pago'] = 1;
        $this->cita->editar($dataCita, $_SESSION['cita_id']);
        $detallesSerializados = (serialize($this->session->userdata("detalles")));
        $detallesUnSerializados = unserialize($detallesSerializados);

        foreach ($detallesUnSerializados as $detalle) {
            $datos['cantidad'] = $detalle->cantidad;
            $datos['descripcion'] = $detalle->descripcion;
            $datos['precio'] = $detalle->pvp;
            $datos['total'] = $detalle->total;
            $datos['factura_id'] = $id;
            $datos['servicio_id'] = $detalle->servicio_id;
            $this->detalle_factura->grabar($datos);
        }
        $this->imprimir();
        redirect('cita_medica_controller/buscar_citas');
    }

    function buscar() {
        $data['lista'] = $this->factura->buscar($this->input->post("criterio"));
        $data['title'] = 'Facturas';
        $data['main'] = 'frm_facturas';
        $this->load->view('/inicio/header', $data);
        $this->load->view('/frm_principal_admin', $data);
    }

}
